Software Tools for Earth and Environmental Science

– 13th WEEK –

Emir Toker

15/01/2020

R Probability

  • Syllabus and Final Project
  • R Repeat
    • R, R-Studio, R-Files
    • Data Types and Structures
    • Conditions, Loops and Functions
    • Statistic and Probability
  • Practice : Function and If
  • Practice : NetCDF and shapefile
  • Next Week

Syllabus and Final Project

Syllabus

Final Project

29 January 2020, 09:00-11:30

  • Download the data
  • Open a new R-Project and R-Script
  • Read .txt, .csv, and .nc file
  • Manipulate and plot the data
  • Use if, for and function
  • Export as R-Notebook

R Repeat

R, R-Studio, R-Files

R Homepage - LINK

The Comprehensive R Archive Network (CRAN) - LINK

R, R-Studio, R-Files

Turkey - Middle East Technical University Northern Cyprus Campus, Mersin - LINK

R

RStudio Desktop Download

RStudio

An Integrated Development Environment (IDE) for R.

Project

File - New Project

R Files

R Notebook

Packages

Install, Update

R for Basic Math

25 * 4 + 9 / 3 - 56

Assigning Objects

The assignment arrow (<-)

Comments

How to tell R to ignore a part of your code? (#)

Data Types

  • Numeric (Double)

  • Integer

  • Complex

  • Logical

  • Character

  • Special Values

  • Date/Time

  • Arithmetic Operators ( +, -, /, x )

  • Logical Operators ( <, >, ==, != ….)

Coercion

Data Structures (R-Objects)

Arrays and Data Frames

Create a Function

Read

Import Dataset -> From Text (readr) -> Browse

Read and write

Console

  • read.table()

  • read.delim()

  • read.csv()

  • write.table()

Plot

Uptade Preview

Repeat - R Programming (Conditions)

  • Comparison Operators
    • equal (==)
    • not equal (!=)
    • greater or equal to (>=)
    • less or equal to (<=)
  • Logical Operators
    • the and operator (&)
    • the or operator (|)
    • the not operator (!)
  • if (Stand-Alone) Statement
  • else Statement
  • else if Statement

if (Stand-Alone) Statement

-else - and -else if- Statement

If your situation has more than two mutually exclusive cases, use else and if statements together.

Nesting and Stacking Statements

R Programming - Loops

R Programming - repeat Loops

R Programming - while Loops

R Programming - for Loops

R Programming - apply Groups

  • The apply() functions form the basis of more complex combinations and helps to perform operations with very few lines of code.
  • More specifically, the family is made up of the apply(), lapply() , sapply(), vapply(), mapply(), rapply(), and tapply() functions.

R Elemantary Statistics

R Elemantary Statistics

  • Centrality: Mean, Median, Mode
  • Quantiles, Percentiles, and the Five-Number Summary
  • Spread: Variance, Standard Dev., Interquartile Range
  • Covariance and Correlation

Basic Data Visualization

  • Barplots and Pie Charts
  • Histogram
  • Boxplot
  • Scatter Plots

R - Probability

  • Qualitative and Quantitative Data
  • Discrete and Continuous Data
  • Probability ‘Density vs Mass’ Function (PDF vs PMF)
  • Cumulative Distribution Function (CDF)
  • Shape of PDF : Symmetry, Skewness, Modality, Kurtosis
  • Common Probability Distributions

My density distribution

lower < 7 < upper

X >= 2  &  X <= 7
(X[lower] - 1)/36

X > 7 & X <= 12
13 - X[upper])/36

PDF - Probability Density Function

Practice : Write A Function

Practice : Write A Function - 1

  • I have four different numbers w,x,y,z
  • I want to define them OUT of function
  • I want to calculate w+x IN function
  • I want to calculate y*z IN function
  • I want to print the results IN function
w <-
x <-
y <-
z <-

my_fun1 <- function(){



print(result1)
print(result2)

}

Practice : Write A Function - 1

  • I have four different numbers w,x,y,z
  • I want to define them OUT of function
  • I want to calculate w+x IN function
  • I want to calculate y*z IN function
  • I want to print the results IN function
w <- 
x <- 
y <- 
z <- 

my_fun1 <- function(){

result1 <-
result2 <-

print(result1)
print(result2)

}

Practice : Write A Function - 1

  • I have four different numbers w,x,y,z
  • I want to define them OUT of function
  • I want to calculate w+x IN function
  • I want to calculate y*z IN function
  • I want to print the results IN function
w <- 
x <- 
y <- 
z <- 

my_fun1 <- function(){

result1 <- w+x
result2 <- y*z

print(result1)
print(result2)

}

Practice : Write A Function - 1

  • I have four different numbers w,x,y,z
  • I want to define them OUT of function
  • I want to calculate w+x IN function
  • I want to calculate y*z IN function
  • I want to print the results IN function
w <- 1
x <- 2
y <- 3
z <- 4

my_fun1 <- function(){

result1 <- w+x
result2 <- y*z

print(result1)
print(result2)

}

Practice : Write A Function - 1

  • I have four different numbers w,x,y,z
  • I want to define them OUT of function
  • I want to calculate w+x IN function
  • I want to calculate y*z IN function
  • I want to print the results IN function
w <- 1
x <- 2
y <- 3
z <- 4

my_fun1 <- function(){
result1 <- w+x
result2 <- y*z
print(result1)
print(result2)
}

my_fun1()
## [1] 3
## [1] 12

Practice : Write A Function - 2

  • I have four different numbers w,x,y,z
  • I want to define them IN of function
  • I want to calculate w+x IN function
  • I want to calculate y*z IN function
  • I want to print the results IN function
my_fun2 <- function(){
  
w <- 1
x <- 2
y <- 3
z <- 4

result1 <- w+x
result2 <- y*z

print(result1)
print(result2)

}

Practice : Write A Function - 3

  • I have four different numbers w,x,y,z
  • I want to define them WHEN I am using the function
  • I want to calculate w+x IN function
  • I want to calculate y*z IN function
  • I want to print the results IN function
my_fun3 <- function(w,x,y,z){
  
result1 <- w+x
result2 <- y*z

print(result1)
print(result2)

}

# my_fun3()
my_fun3(1,2,3,4)
## [1] 3
## [1] 12

BONUS

  • menu() function
menu(c("Yes", "No"), title="What dou you think?")
menu1 <- menu(c("Yes", "No"), title="What dou you think?")
menu1

menu(c("Option1","Option2","Option3","Option4"), title="Choose one of them")
menu2 <- menu(c("Option1","Option2","Option3","Option4"), title="Choose one of them")
menu2

Practice : Write A Function - 3

  • I have two different numbers x,y
  • I want to define them WHEN I am using the function
  • I want to define ONE calculation DURING the function
  • I want to print the results IN function
my_fun3 <- function(x,y){

math <- menu(c("+", "-", "*", "/"), title="Which calculation?")

  if (math==1) {
    result <- x+y
  } else if ( ) {
     
  } else if ( ) {
    
  } else  {
    
  }

print(result)
}

Practice : Write An IF-Statement

Practice : Write An IF-Statement - 1

  • Write your own if-statement and condition

if ( condition ) {

print( )

}

Practice : Write An IF-Statement - 2

  • Two answers
  • Two options
  • One Result
worry_flow <- function() {

answer1 <- menu()

  if () {
  
    print()
  
  }
  
  else {
  
    answer2 <- menu()

  }
  
}

Practice : Write An IF-Statement - 2

  • Two answers
  • Two options
  • One Result
worry_flow <- function() {

answer1 <- menu()
  if () {
      print()
  }
  else {
      answer2 <- menu()
      if(){
          print()
      }
      else{
      }
  }
}

Practice : Write An IF-Statement - 2

  • Two answers
  • Two options
  • One Result
worry_flow <- function() {

answer1 <-  menu(c("Yes","No"), title = "Do you have a problem in your life ?")
  if (answer1 == 2) {
      print("Then Don't Worry")
  }
  else {
      answer2 <- menu(c("Yes","No"), title = "Can you do something about it ?")
      if(answer2 == 1){
          print("Then Don't Worry")
      }
      else{
      print("Then Don't Worry")
      }
  }
}

Practice : Write An IF-Statement - 2

  • Two answers
  • Two options
  • One Result
worry_flow <- function() {

answer1 <-  menu(c("Yes","No"), title = "Do you have a problem in your life ?")
  if (answer1 == 2) {
      print("Then Don't Worry")
  }
  else {
      answer2 <- menu(c("Yes","No"), title = "Can you do something about it ?")
      if(answer2 == 1){
          print("Then Don't Worry")
      }
      else{
      print("Then Don't Worry")
      }
  }

require(tcltk)
msgBox <- tkmessageBox(title = "Title of message box",
                       message = "THEN WYH WORRY!", 
                       icon = "info", 
                       type = "ok")

}

R - NETCDF

R - NETCDF

CRU_TR_Near-Surface_Temp_16-01-1901_16-12-2012_Monthly

R - NETCDF

CRU_TR_Near-Surface_Temp_16-01-1901_16-12-2012_Monthly

R - NETCDF

install.packages("ncdf4")
install.packages("RNetCDF")
install.packages("maptools")
install.packages("fields")

R Advance - ncdf4 Package

R Advance - ncdf4 Package

library(ncdf4)
ncdf4_open <- nc_open("cru_1901_2012_tmp_TR.nc")
ncdf4_open
## File cru_1901_2012_tmp_TR.nc (NC_FORMAT_CLASSIC):
## 
##      1 variables (excluding dimension variables):
##         double tmp[lon,lat,time]   
##             long_name: near-surface temperature
##             units: degrees Celsius
##             _FillValue: 9.96920996838687e+36
##             missing_value: 9.96920996838687e+36
##             correlation_decay_distance: 1200
## 
##      3 dimensions:
##         lon  Size:42
##             standard_name: longitude
##             long_name: longitude
##             units: degrees_east
##             axis: X
##         lat  Size:18
##             standard_name: latitude
##             long_name: latitude
##             units: degrees_north
##             axis: Y
##         time  Size:1344   *** is unlimited ***
##             standard_name: time
##             long_name: time
##             units: days since 1900-01-01 00:00:00
##             calendar: standard
## 
##     10 global attributes:
##         CDI: Climate Data Interface version 1.6.9 (http://mpimet.mpg.de/cdi)
##         Conventions: CF-1.4
##         history: Mon Feb 22 16:24:47 2016: cdo sellonlatbox,25,46,34,43 cru_ts3.21.1901.2012.tmp.dat.nc cru_1901_2012_tmp_TR.nc
## Wed 10 Jul 2013 17:04:15 BST : User ianharris : Program makegridsauto.for called by update.for
##         source: Run ID = 1307101324
## Data generated by BADC from:
## tmp.1307101049.dtb
##         institution: Data held at British Atmospheric Data Centre, RAL, UK.
##         title: CRU TS3.21 Mean Temperature
##         references: Information on the data is available at http://badc.nerc.ac.uk/data/cru/
##         comment: Data restrictions: for academic research use only.Contact BADC for details
##         contact: BADC <badc@rl.ac.uk>
##         CDO: Climate Data Operators version 1.7.0rc2 (http://mpimet.mpg.de/cdo)

R Advance - RNetCDF Package

R Advance - RNetCDF Package

library(RNetCDF)
rnetcdf_open <- open.nc("cru_1901_2012_tmp_TR.nc")
print.nc(rnetcdf_open)
## netcdf classic {
## dimensions:
##  lon = 42 ;
##  lat = 18 ;
##  time = UNLIMITED ; // (1344 currently)
## variables:
##  NC_DOUBLE lon(lon) ;
##      NC_CHAR lon:standard_name = "longitude" ;
##      NC_CHAR lon:long_name = "longitude" ;
##      NC_CHAR lon:units = "degrees_east" ;
##      NC_CHAR lon:axis = "X" ;
##  NC_DOUBLE lat(lat) ;
##      NC_CHAR lat:standard_name = "latitude" ;
##      NC_CHAR lat:long_name = "latitude" ;
##      NC_CHAR lat:units = "degrees_north" ;
##      NC_CHAR lat:axis = "Y" ;
##  NC_DOUBLE time(time) ;
##      NC_CHAR time:standard_name = "time" ;
##      NC_CHAR time:long_name = "time" ;
##      NC_CHAR time:units = "days since 1900-01-01 00:00:00" ;
##      NC_CHAR time:calendar = "standard" ;
##  NC_DOUBLE tmp(lon, lat, time) ;
##      NC_CHAR tmp:long_name = "near-surface temperature" ;
##      NC_CHAR tmp:units = "degrees Celsius" ;
##      NC_DOUBLE tmp:_FillValue = 9.96920996838687e+36 ;
##      NC_DOUBLE tmp:missing_value = 9.96920996838687e+36 ;
##      NC_DOUBLE tmp:correlation_decay_distance = 1200 ;
## 
## // global attributes:
##      NC_CHAR :CDI = "Climate Data Interface version 1.6.9 (http://mpimet.mpg.de/cdi)" ;
##      NC_CHAR :Conventions = "CF-1.4" ;
##      NC_CHAR :history = "Mon Feb 22 16:24:47 2016: cdo sellonlatbox,25,46,34,43 cru_ts3.21.1901.2012.tmp.dat.nc cru_1901_2012_tmp_TR.nc
## Wed 10 Jul 2013 17:04:15 BST : User ianharris : Program makegridsauto.for called by update.for" ;
##      NC_CHAR :source = "Run ID = 1307101324
## Data generated by BADC from:
## tmp.1307101049.dtb" ;
##      NC_CHAR :institution = "Data held at British Atmospheric Data Centre, RAL, UK." ;
##      NC_CHAR :title = "CRU TS3.21 Mean Temperature" ;
##      NC_CHAR :references = "Information on the data is available at http://badc.nerc.ac.uk/data/cru/" ;
##      NC_CHAR :comment = "Data restrictions: for academic research use only.Contact BADC for details" ;
##      NC_CHAR :contact = "BADC <badc@rl.ac.uk>" ;
##      NC_CHAR :CDO = "Climate Data Operators version 1.7.0rc2 (http://mpimet.mpg.de/cdo)" ;
## }

R Advance - ncdf4 VS RNetCDF

ncdf4_open <- nc_open("data.nc")
class(ncdf4_open)
str(ncdf4_open)
attributes(ncdf4_open)
attributes(ncdf4_open$dim)
attributes(ncdf4_open$var)
attributes(ncdf4_open$var$tmp)

ncdf4_open$var$tmp$longname
ncdf4_open$var$tmp$name
ncdf4_open$var$tmp
ncatt_get(ncdf4_open,'tmp')
ncvar_get(ncdf4_open,"tmp")
ncdf4_tmp <- ncvar_get(ncdf4_open,"tmp")

class(ncdf4_tmp)
dim(ncdf4_tmp)
str(ncdf4_tmp)

attributes(ncdf4_open)
attributes(ncdf4_open$dim)
ncdf4_lon <- ncvar_get(ncdf4_open, ncdf4_open$dim$lon)
ncdf4_lat <- ncvar_get(ncdf4_open, ncdf4_open$dim$lat)
ncdf4_time<- ncvar_get(ncdf4_open, ncdf4_open$dim$time)
rnetcdf_open <- open.nc("data.nc")
class(rnetcdf_open)
str(rnetcdf_open)

rnetcdf_read <- read.nc(rnetcdf_open)
class(rnetcdf_read)
str(rnetcdf_read)
attributes(rnetcdf_read)

var.get.nc(rnetcdf_open,'tmp')
rnetcdf_tmp <- var.get.nc(rnetcdf_open,'tmp')

class(rnetcdf_tmp)
dim(rnetcdf_tmp)
str(rnetcdf_tmp)
attributes(rnetcdf_open)
attributes(rnetcdf_read)

rnetcdf_lon <- var.get.nc(rnetcdf_open, "lon")
rnetcdf_lat <- var.get.nc(rnetcdf_open, "lat")
rnetcdf_time<- var.get.nc(rnetcdf_open, "time")

R Advance - ncdf4 Package

ncdf4_tmp <- ncvar_get(ncdf4_open,"tmp")
ncdf4_tmp[,,1]
ncdf4_tmp_first<- ncdf4_tmp[,,1]

image(ncdf4_lon, ncdf4_lat, ncdf4_tmp_first)

R Advance - ncdf4 Package

library("maptools")

turkey_shp <- readShapePoly("turkiye.shp")

image(ncdf4_lon, ncdf4_lat, ncdf4_tmp_first)

plot(turkey_shp, add = T)

R Advance - ncdf4 Package

library("fields")

image.plot( ncdf4_lon, ncdf4_lat, ncdf4_tmp_first, 
              xlab = "Lon", 
              ylab = "Lat", 
              main = "First Step (01-1901) Temperature for Turkey" )

plot(turkey_shp, add = T) 

apply(array1[,,index1],1:2,mean)

R - NETCDF

image.plot( ncdf4_lon, ncdf4_lat, 
            apply(ncdf4_tmp[,,1332:1344],1:2,mean) - apply(ncdf4_tmp[,,1:1331],1:2,mean), 
            xlab = "Lon", 
            ylab = "Lat", 
            main = "? for Turkey" )
         
plot(turkey_shp, add = T) 

R - NETCDF

image.plot( ncdf4_lon, ncdf4_lat, 
            apply(ncdf4_tmp[,,1332:1344],1:2,mean) - apply(ncdf4_tmp[,,1:1331],1:2,mean), 
            xlab = "Lon", 
            ylab = "Lat", 
            main = " 2012 VS Mean of 1901-2011 for Turkey" )
         
plot(turkey_shp, add = T) 

Next Week

Next Week

  • Assessment Test before Final